<template>
  <div class="nav-menu">
    <div class="logo">
      <img class="img" src="~@/assets/img/logo.svg" alt="logo" />
      <span v-if="!collapse" class="title">Vue3+TS+VueX</span>
    </div>
    <el-menu
      :default-active="defaultValue"
      :collapse="collapse"
      class="el-menu-vertical"
      background-color="#0c2135"
      text-color="#b7bdc3"
      active-text-color="#0a60bd"
    >
      <template v-for="item in userMenus" :key="item.id">
        <!-- 含有子菜单 -->
        <template v-if="item.type === 1">
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <el-icon
                v-if="item.id == 38"
                :class="{ 'active-menu': parentMenuId == 38 && collapse }"
              >
                <Odometer />
              </el-icon>
              <el-icon
                v-else-if="item.id == 1"
                :class="{ 'active-menu': parentMenuId == 1 && collapse }"
                ><Setting
              /></el-icon>
              <el-icon
                v-else-if="item.id == 9"
                :class="{ 'active-menu': parentMenuId == 9 && collapse }"
                ><Discount
              /></el-icon>
              <el-icon
                v-else
                :class="{ 'active-menu': parentMenuId == 41 && collapse }"
                ><MessageBox
              /></el-icon>
              <span>{{ item.name }}</span>
            </template>
            <!-- 遍历里面的item -->
            <template v-for="subitem in item.children" :key="subitem.id">
              <el-menu-item
                :index="subitem.id + ''"
                @click="handleMenuItemClick(subitem)"
              >
                <span>{{ subitem.name }}</span>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <!-- 一级菜单 -->
        <template v-else-if="item.type === 2">
          <el-menu-item :index="item.id + ''">
            <span>{{ item.name }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter, useRoute } from 'vue-router'
import { pathMapToMenu } from '@/utils/map-menu'
import type { IBreadcrumb } from '@/base-ui/breadcrumb/types'
export default defineComponent({
  props: {
    collapse: {
      type: Boolean,
      default: false
    }
  },
  setup() {
    const store = useStore()
    const router = useRouter()

    const userMenus = computed(() => {
      return store.state.login.userMenus
    })

    const parentMenuId = computed(() => {
      let breadcrumbs: IBreadcrumb[] = []
      pathMapToMenu(userMenus.value, route.path, breadcrumbs)
      return breadcrumbs[0].id
    })

    const route = useRoute()
    let menu = pathMapToMenu(userMenus.value, route.path)
    const defaultValue = ref(menu?.id + '')
    const handleMenuItemClick = (subItem: any) => {
      console.log('路由是:', subItem.url)
      router.replace({
        path: subItem.url ?? '/not-found'
      })
    }

    return {
      userMenus,
      defaultValue,
      parentMenuId,
      handleMenuItemClick
    }
  }
})
</script>

<style scoped lang="less">
.nav-menu {
  height: 100%;
  background-color: #001529;

  .active-menu {
    color: #0a60bd;
  }

  .logo {
    display: flex;
    height: 48px;
    padding: 12px 10px 8px 10px;
    align-items: center;

    .img {
      height: 100%;
      margin: 0 10px;
    }

    .title {
      font-size: 8px;
      font-weight: 700;
      color: white;
      flex: 1;
    }
  }

  .el-menu {
    border-right: none !important;
  }

  // 目录
  .el-sub-menu {
    background-color: #001529 !important;
    // 二级菜单 ( 默认背景 )
    .el-menu-item {
      padding-left: 50px !important;
      background-color: #0c2135 !important;
    }
  }

  ::v-deep .el-sub-menu__title {
    background-color: #001529 !important;
  }

  // hover 高亮
  .el-menu-item:hover {
    color: #fff !important; // 菜单
  }

  .el-menu-item.is-active {
    color: #fff !important;
    background-color: #0a60bd !important;
  }
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 100%;
  height: calc(100% - 48px);
}
</style>
